/**** 

Goal: 
Generate the blockholder network and merge it with the SC network.

File inputs: 
> gvkey_cik.dta (Link between gvkeys and CIKs from Compustat) 

File outputs: 
> block_network.dta (Blockholder network)

****/ 

global dir ""
if c(os) == "MacOSX" {
    global dir "/Users/ispyrido"
}
else if c(os) == "Windows" {
    global dir "C:"
}

cd "$dir/Dropbox/My Projects/0) Financial Constraints_/Data and Code"
set processors 4

*** Prepare the CIK and GVKEY LINK: 
use "gvkey_cik", clear
	keep cik gvkey fyear
	order cik gvkey fyear
	duplicates drop
	destring cik gvkey, replace
	drop if cik==. | gvkey==.
	gen year = fyear
	sort gvkey fyear
save "gvkey_cik2", replace 


import delimited "blockholders.csv", clear

	format company_name %-70s

	order  company_cik year blockholder_cik 
	sort company_cik year blockholder_cik
	duplicates drop company_cik year blockholder_cik, force
	isid company_cik year blockholder_cik
	gen fyear = year

	*** merge the cik of GVKEY 1:
	gen cik = company_cik
	merge m:1 cik year using "gvkey_cik2", keep(1 3) gen(_merge_cik1) keepusing(gvkey)
	rename (cik gvkey) (cik1 gvkey1)
	*** merge the cik of GVKEY 2:
	gen cik = blockholder_cik
	merge m:1 cik year using "gvkey_cik2", keep(1 3) gen(_merge_cik2) keepusing(gvkey)
	rename (cik gvkey) (cik2 gvkey2)

	tab _merge_cik1 _merge_cik2

	keep if gvkey1!=. & gvkey2 !=.

	save "block_network", replace
	export delimited using "block_network.csv", replace


***************************************************************************************************
/*** SAS: CREATE THE NETWORK OF BLOCKHOLDERS IN SAS ; 

proc import datafile= "C:/Dropbox/My Projects/0) Financial Constraints/Data and Code/SCnetwork2.csv"
     out= SCnetwork2
     dbms=csv
     replace;
     getnames=yes;
run;
proc import datafile= "C:/Dropbox/My Projects/0) Financial Constraints_/Data and Code/block_network.csv"
     out= block_network
     dbms=CSV
     replace;
     getnames=yes;
run;
* Combine the two networks; 
	 proc sql;
	    create table SC_blocks as
	    select distinct a.*, max(b.position) as position_2_to_1, 
	    					 max(c.position) as position_1_to_2
	    from SCnetwork2 as a 
	    	left join block_network as b on a.gvkey1=b.gvkey1 and a.gvkey2=b.gvkey2 and /* b.fyear = a.fyear  */ between a.fyear-1 and a.fyear+1
	    	left join block_network as c on a.gvkey1=c.gvkey2 and a.gvkey2=c.gvkey1 and /* c.fyear = a.fyear  */ between a.fyear-1 and a.fyear+1
	    group by a.gvkey1, a.fyear, a.gvkey2
	    order by a.gvkey1, a.fyear, a.gvkey2
		;
	quit; 
	proc export data = SC_blocks
				file = "C:/Dropbox/My Projects/0) Financial Constraints_/Data and Code/SC_blocks.dta"
				dbms = STATA REPLACE;
	run;
***************************************************************************************************/


set processors 4
use "sc_blocks", clear // Created in SAS

	gen common_block = (position_2_to_1!=. | position_1_to_2!=.)
	gen position_2_to_1s_d= (position_2_to_1>0 & position_2_to_1!=.)
	gen position_1_to_2s_d= (position_1_to_2>0 & position_1_to_2!=.)
	*** COLLAPSE AT THE FIRM LEVEL:
	egen common_block_max   = max(common_block), by(gvkey1 fyear)
	egen common_block_max_S = max(common_block / (supplier==1)), by(gvkey1 fyear)
	egen common_block_max_C = max(common_block / (supplier==0)), by(gvkey1 fyear)
	*** 
	egen position_2_to_1_max   = max(position_2_to_1s_d), by(gvkey1 fyear)
	egen position_2_to_1_max_S = max(position_2_to_1s_d / (supplier==1) ), by(gvkey1 fyear)
	egen position_2_to_1_max_C = max(position_2_to_1s_d / (supplier==0) ), by(gvkey1 fyear)
	*** 
	egen position_1_to_2_max   = max(position_1_to_2s_d), by(gvkey1 fyear)
	egen position_1_to_2_max_S = max(position_1_to_2s_d / (supplier==1) ), by(gvkey1 fyear)
	egen position_1_to_2_max_C = max(position_1_to_2s_d / (supplier==0) ), by(gvkey1 fyear)

	rename gvkey1 gvkey 
	keep gvkey fyear common_block_max common_block_max_S common_block_max_C position_2_to_1_max position_2_to_1_max_S position_2_to_1_max_C position_1_to_2_max position_1_to_2_max_S position_1_to_2_max_C
	duplicates drop

save "sc_blocks_collapsed", replace 
